#!/bin/bash

# 设置标题
echo -e "\033]0;UPMS和SGR数据库迁移脚本生成工具\007"

# 设置控制台编码为UTF-8
export LANG=en_US.UTF-8

# 获取脚本所在目录，移除末尾的斜杠
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
SCRIPT_DIR=${SCRIPT_DIR%/}

# 确保脚本在解决方案根目录下的docs文件夹中运行
if [[ "${SCRIPT_DIR: -4}" != "docs" ]]; then
    echo "错误：请确保脚本位于解决方案根目录下的docs文件夹中！"
    echo "当前目录：$SCRIPT_DIR"
    read -p "按任意键继续..."
    exit 1
fi

# 获取解决方案根目录
SOLUTION_DIR=$(readlink -f "$SCRIPT_DIR/..")

# 设置关键目录
PROJECT_DIR="$SOLUTION_DIR/src/Apps/Sgr.Admin.WebHost"
UPMS_MIGRATIONS_DIR="$PROJECT_DIR/Infrastructure/Migrations/Upms"
SGR_MIGRATIONS_DIR="$PROJECT_DIR/Infrastructure/Migrations/Sgr"
SQL_DIR="$SOLUTION_DIR/docs/sql"

# 显示配置信息
echo "[配置信息]"
echo "脚本目录: $SCRIPT_DIR"
echo "解决方案目录: $SOLUTION_DIR"
echo "项目目录: $PROJECT_DIR"
echo "UPMS迁移目录: $UPMS_MIGRATIONS_DIR"
echo "SGR迁移目录: $SGR_MIGRATIONS_DIR"
echo "SQL输出目录: $SQL_DIR"
echo

# 验证目录结构
echo "[目录验证]"
if [[ ! -d "$SOLUTION_DIR/src" ]]; then
    echo "错误：未找到src目录，请确保脚本在正确的位置运行！"
    read -p "按任意键继续..."
    exit 1
fi

if [[ ! -d "$PROJECT_DIR" ]]; then
    echo "错误：未找到项目目录："
    echo "$PROJECT_DIR"
    read -p "按任意键继续..."
    exit 1
fi

if [[ ! -f "$PROJECT_DIR/Sgr.Admin.WebHost.csproj" ]]; then
    echo "错误：未找到项目文件！"
    echo "$PROJECT_DIR/Sgr.Admin.WebHost.csproj"
    read -p "按任意键继续..."
    exit 1
fi

# 验证环境
echo "[环境检查]"
if ! command -v dotnet &> /dev/null; then
    echo "错误：未安装 .NET SDK 或未将其添加到 PATH！"
    read -p "按任意键继续..."
    exit 1
fi

if ! dotnet ef --version &> /dev/null; then
    echo "错误：未安装 Entity Framework Core 工具！"
    echo "请运行：dotnet tool install --global dotnet-ef"
    read -p "按任意键继续..."
    exit 1
fi

# 创建必要的目录
echo "-----------------------------------------------"
echo "                  创建目录"
echo "-----------------------------------------------"
mkdir -p "$UPMS_MIGRATIONS_DIR" || { echo "错误：无法创建UPMS迁移目录！"; read -p "按任意键继续..."; exit 1; }
mkdir -p "$SGR_MIGRATIONS_DIR" || { echo "错误：无法创建SGR迁移目录！"; read -p "按任意键继续..."; exit 1; }
mkdir -p "$SQL_DIR" || { echo "错误：无法创建SQL脚本目录！"; read -p "按任意键继续..."; exit 1; }

# 切换到项目目录
echo "-----------------------------------------------"
echo "                  切换目录"
echo "-----------------------------------------------"
cd "$PROJECT_DIR" || { echo "错误：无法切换到项目目录！"; read -p "按任意键继续..."; exit 1; }

echo "-----------------------------------------------"
echo "                  准备就绪"
echo "-----------------------------------------------"
echo "当前工作目录: $(pwd)"
echo

# 询问用户是否继续
read -p "是否开始生成迁移脚本？(Y/N) " CONTINUE
if [[ ! "$CONTINUE" =~ ^[Yy]$ ]]; then
    echo "操作已取消。"
    read -p "按任意键继续..."
    exit 0
fi

echo
echo "[开始执行]"

# ===== UPMS迁移处理 =====
echo "[处理UPMS迁移]"
# 检查是否存在现有UPMS迁移
if [[ -n "$(ls -A "$UPMS_MIGRATIONS_DIR")" ]]; then
    echo "删除现有UPMS迁移..."
    dotnet ef migrations remove --context UpmsDbContext || { echo "错误：删除UPMS迁移失败！"; read -p "按任意键继续..."; exit 1; }
    echo "√ 删除UPMS迁移完成"
fi

echo "添加UPMS迁移..."
dotnet ef migrations add InitialUpmsSchema --context UpmsDbContext --output-dir Infrastructure/Migrations/Upms || { echo "错误：添加UPMS迁移失败！"; read -p "按任意键继续..."; exit 1; }
echo "√ 添加UPMS迁移完成"
echo

# 生成UPMS SQL脚本
echo "生成UPMS标准SQL脚本..."
dotnet ef migrations script --context UpmsDbContext --output "$SQL_DIR/upmsdb.sql" || { echo "错误：生成UPMS SQL脚本失败！"; read -p "按任意键继续..."; exit 1; }
echo "√ 生成UPMS标准SQL脚本完成"

echo "生成UPMS幂等SQL脚本..."
dotnet ef migrations script --idempotent --context UpmsDbContext --output "$SQL_DIR/idempotent_upmsdb.sql" || { echo "错误：生成UPMS幂等SQL脚本失败！"; read -p "按任意键继续..."; exit 1; }
echo "√ 生成UPMS幂等SQL脚本完成"
echo

# ===== SGR迁移处理 =====
echo "[处理SGR迁移]"
# 检查是否存在现有SGR迁移
if [[ -n "$(ls -A "$SGR_MIGRATIONS_DIR")" ]]; then
    echo "删除现有SGR迁移..."
    dotnet ef migrations remove --context SgrDbContext || { echo "错误：删除SGR迁移失败！"; read -p "按任意键继续..."; exit 1; }
    echo "√ 删除SGR迁移完成"
fi

echo "添加SGR迁移..."
dotnet ef migrations add InitialSgrSchema --context SgrDbContext --output-dir Infrastructure/Migrations/Sgr || { echo "错误：添加SGR迁移失败！"; read -p "按任意键继续..."; exit 1; }
echo "√ 添加SGR迁移完成"
echo

# 生成SGR SQL脚本
echo "生成SGR标准SQL脚本..."
dotnet ef migrations script --context SgrDbContext --output "$SQL_DIR/sgrdb.sql" || { echo "错误：生成SGR SQL脚本失败！"; read -p "按任意键继续..."; exit 1; }
echo "√ 生成SGR标准SQL脚本完成"

echo "生成SGR幂等SQL脚本..."
dotnet ef migrations script --idempotent --context SgrDbContext --output "$SQL_DIR/idempotent_sgrdb.sql" || { echo "错误：生成SGR幂等SQL脚本失败！"; read -p "按任意键继续..."; exit 1; }
echo "√ 生成SGR幂等SQL脚本完成"
echo

echo "[执行完成]"
echo "SQL脚本已生成到以下位置："
echo "[UPMS]"
echo "- 标准脚本：$SQL_DIR/upmsdb.sql"
echo "- 幂等脚本：$SQL_DIR/idempotent_upmsdb.sql"
echo "[SGR]"
echo "- 标准脚本：$SQL_DIR/sgrdb.sql"
echo "- 幂等脚本：$SQL_DIR/idempotent_sgrdb.sql"
echo

read -p "按任意键继续..."